package test0109;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test07 {
    public static void main(String[] args) {

        // (7)list集合按照要求存储到map中

        M m1 = new M("张三",30);
        M m2 = new M("李四",40);
        M m3 = new M("王五",50);

        List<M> list = new ArrayList<M>();

        list.add(m1);
        list.add(m2);
        list.add(m3);

        // map要求一的结构
        // key      vaule
        // 张三 ->   m1对象
        // 李四 ->   m2对象
        // 王五 ->   m3对象
        Map<String, M> map = new HashMap<String, M>();
        list.forEach(a -> {
            map.put(a.getName(),a);
        });
        map.forEach((k,v)->{
            System.out.println(k);
        });


        // map要求二的结构
        // key      vaule
        // 30 ->   m1对象
        // 40 ->   m2对象
        // 50 ->   m3对象
        Map<Integer, M> map1 = new HashMap<Integer, M>();
        list.forEach(a -> {
            map1.put(a.getAge(),a);
        });
        map1.forEach((k,v)->{
            System.out.println(k);
        });


    }
}

class M {
    String name;
    int age;

    public M() {
    }

    public M(String name,int age) {
        this.age = age;
        this.name = name;
    }
    public M(int age,String name) {
        this.age = age;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "M{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
